#!/usr/bin/env python
"""Module responsible for generating vulnerabilites from docker containers."""

import os
import subprocess

from components import topology_parser as top_par
from components import writer

def parse_vulnerabilities(folder):
    """Function that gets the vulnerabilities for each docker container."""

    print("Executing the vulnerability parser...")

    list_services = top_par.get_services(folder)
    mapping_image_names = top_par.get_mapping_service_to_image_names(folder)

    old_root_path = os.getcwd()

    if not os.path.exists(os.path.join(old_root_path, "examples-results")):
        os.makedirs(os.path.join(old_root_path, "examples-results"), mode=0o777)


    if not os.path.exists(os.path.join(old_root_path, "examples-results", folder)):
        os.makedirs(os.path.join(old_root_path, "examples-results", folder), mode=0o777)

    os.chdir(os.path.join(os.getcwd(), folder))

    # Build the images that are specified in docker-compose.yml
    subprocess.call(["docker-compose",
                     "build"])

    # Start clair, clairctl and postgres
    clairctl_home = os.path.join("/usr",
                                 "local",
                                 "go",
                                 "bin",
                                 "src",
                                 "github.com",
                                 "jgsqware",
                                 "clairctl")

    os.chdir(clairctl_home)

    subprocess.call(["docker-compose",
                     "up",
                     "-d"])

    # Doing the analyzis and make reports files.
    for service in list_services:

        image = mapping_image_names[service]

        print("Pushing the image "+image)
        subprocess.call(["docker-compose",
                         "exec",
                         "--user",
                         "root",
                         "clairctl",
                         "clairctl",
                         "push",
                         "--local",
                         image], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        print("Analyzing the image "+image)
        subprocess.call(["docker-compose",
                         "exec",
                         "--user",
                         "root",
                         "clairctl",
                         "clairctl",
                         "analyze",
                         "--local",
                         image])

        print("Making report for the image "+image)
        subprocess.call(["docker-compose",
                         "exec",
                         "--user",
                         "root",
                         "clairctl",
                         "clairctl",
                         "report",
                         "--local",
                         "--format",
                         "json",
                         image], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        writer.copy_vulnerability_file(clairctl_home, image, old_root_path, folder)

    os.chdir(old_root_path)

    print("Vulnerability parser executed.")
